package com.lepu.app.fun.my.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.AppManager;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.lepu.app.fun.index.bean.BeanIndexNew;
import com.lepu.app.fun.my.MyArticleInfoActivity;
import com.lepu.app.fun.my.MyVideoActivity;
import com.lepu.app.fun.my.bean.BeanMyArticle;
import com.lepu.pasm.R;

public class AdapterMyVideo extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private Context mContext = null;
	private ArrayList<BeanMyArticle> mArrayList = null;
	private boolean mIsCollect = false;

	public AdapterMyVideo(Context context, ArrayList<BeanMyArticle> arrayList,boolean isCollect) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
		mIsCollect = isCollect;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.my_video_activity_listitem, null);

			holder = new ViewHolder();

			LinearLayout mainItemLayout = (LinearLayout) convertView
					.findViewById(R.id.mainItemLayout);
			ImageView iconImageView = (ImageView) convertView
					.findViewById(R.id.iconImageView);
			TextView titleTextView = (TextView) convertView
					.findViewById(R.id.titleTextView);
			TextView timeTextView = (TextView) convertView
					.findViewById(R.id.timeTextView);
			TextView collectTextView = (TextView) convertView
					.findViewById(R.id.collectTextView);
			FrameLayout playLayout = (FrameLayout) convertView
					.findViewById(R.id.playLayout);
			TextView playTimesTextView = (TextView) convertView
					.findViewById(R.id.playTimesTextView);
			ImageView pointImageView = (ImageView) convertView
					.findViewById(R.id.pointImageView);

			holder.mainItemLayout = mainItemLayout;
			holder.iconImageView = iconImageView;
			holder.titleTextView = titleTextView;
			holder.timeTextView = timeTextView;
			holder.collectTextView = collectTextView;
			holder.playLayout = playLayout;
			holder.playTimesTextView = playTimesTextView;
			holder.pointImageView = pointImageView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final BeanMyArticle item = mArrayList.get(position);
		
		UniversalImageLoadTool.disPlay(item.Cover, holder.iconImageView,
				R.drawable.default_icon_small);

		if(mIsCollect)
		{
			holder.collectTextView.setVisibility(View.GONE);
			holder.playTimesTextView.setVisibility(View.GONE);
			
			holder.titleTextView.setText(item.Title);
			holder.timeTextView.setText(DateUtilBase.stringFromDate(DateUtilBase
					.dateFromString(item.RecordCreateTime, DateUtilBase.YEAR_MONTH_DAY),
					"MM月dd日"));
			
			holder.pointImageView.setVisibility(View.GONE);
			holder.mainItemLayout.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					
					MyVideoActivity instance = (MyVideoActivity) AppManager.getAppManager().getActivity(MyVideoActivity.class);
					if(instance != null)
					{
						instance.mCurrentBeanMyArticle = item;
						instance.mCurrentOpePosition = position;
						
						Intent intent = new Intent(mContext,
								MyArticleInfoActivity.class);
						intent.putExtra("ArticleID",item.TargetID);
						instance.startActivity(intent, true);
						
					}
				}
			});
		}else{
			holder.titleTextView.setText(item.ArticleTitle);

			holder.timeTextView.setText(DateUtilBase.stringFromDate(DateUtilBase
					.dateFromString(item.PublishDate, DateUtilBase.YEAR_MONTH_DAY),
					"MM月dd日"));
			
			int playCount = UtilityBase.parseInt(item.ViewCount);
			
			holder.collectTextView.setVisibility(View.VISIBLE);
			holder.collectTextView.setText("· 收藏  "+item.CollectCount);
			
			holder.playTimesTextView.setVisibility(View.VISIBLE);
			holder.playTimesTextView.setText(String.format("· 播放  %d", playCount));
			
			holder.mainItemLayout.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					BaseFragmentActivity activity = (BaseFragmentActivity) mContext;
					Intent intent = new Intent(mContext,
							MyArticleInfoActivity.class);
					intent.putExtra("ArticleID",item.ArticleID);
					intent.putExtra("position",position);
					activity.startActivity(intent, true);
					
					BeanIndexNew.insertTargetId(item.ArticleID);
					holder.pointImageView.setVisibility(View.GONE);
				}
			});
			
			//未读加点
			if(BeanIndexNew.isExist(item.ArticleID)){
				holder.pointImageView.setVisibility(View.GONE);
			}else{
				holder.pointImageView.setVisibility(View.VISIBLE);
			}
		}
		
		// 长按监听
		holder.mainItemLayout.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				// 必须空实现，否则外部长按监听不起作用
				return false;
			}
		});

		return convertView;
	}

	@SuppressWarnings("unused")
	private static class ViewHolder {
		LinearLayout mainItemLayout;
		ImageView iconImageView;
		TextView titleTextView;
		TextView timeTextView;
		TextView collectTextView;
		FrameLayout playLayout;
		TextView playTimesTextView;
		ImageView pointImageView;
	}
}
